<template>
	<view>
		<uni-popup ref="popup10" :mask-click="false">
			<view class="uni-tip1">
				<view class="uni-tip-title1">北京中日友好医院</view>
				<view class="uni-tip-content1">急诊科</view>
				<view style="color:#FFFFFF;font-family:SourceHanSansCN-Regular,SourceHanSansCN; font-size: 14.91px;margin: 28.07px 0 22.8px; text-align: center;">正在呼叫…</view>
				<view style="color:#FFFFFF;font-family:SourceHanSansCN-Regular,SourceHanSansCN; font-size: 14.03px; text-align: center;">00:35</view>
				<view class="uni-tip-group-button1">
					<button @click="hujiao(2)">挂断</button>
					<button @click="hujiao(3)">接听</button>
					<!-- <image src="../static/image/jieting@2x.png" @tap="jieting"></image>
									<image src="../static/image/guaduan@2x.png"></image> -->
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import RtmClient from '@/components/rtm-client.js';
	export default {
		data() {
			return {
				// 声网聊天室
				rtm: '',
				channelId: '', // 房间号
				publicChannelId: '', //公共房间号
				destUid: '', // 发给谁
				appid: '9c3a9d03dac14f2cbcb2dcaa63d2a48a',
				uid: '',
				othersChannelId: '', //别人的房间号
				set: ''
			};
		},
		created() {
			 this.getUserData()
		},
		mounted: {},
		methods: {
			childMethod(e, n, r) {
				this.hujiao(e, n, r);
			},
			getUserData() {
				this.$http.post(`/treat/web/v1/user/info`).then(res => {
					// 存储
					getApp().globalData.uid = res.data.userMark;
					this.uid = getApp().globalData.uid;
					this.getChanneId();
					// this.$refs.popup10.open();
				});
			},
			//获取声网聊天室id
			getChanneId() {
				uni.request({
					url: `http://60.205.202.107:39500/agora/v1/publicChannel`,
					success: res => {
						console.log(res.data.msg);
						this.publicChannelId = res.data.msg;
						// // 声网 聊天室
						this.rtm = new RtmClient();
						this.rtm.init(this.appid);
						this.login();
					}
				});
			},
			login(e) {
				if (this.rtm._logined) {
					console.log('You already logined');
					return;
				}
				var params = {
					appID: this.appid,
					channel: this.publicChannelId,
					uid: this.uid,
					token: ''
				};

				try {
					this.rtm.init(params.appID);
					this.rtm
						.login(params.uid, params.token)
						.then(() => {
							console.log('login');
							this.rtm._logined = true;
							this.rtm
								.joinChannel(params.channel)
								.then(() => {
									console.log('joinChannel');
									console.log('进入房间：' + params.channel);
									this.rtm.channels[params.channel].joined = true;
									this.rtm.on('ChannelMessage', ({
										channelName,
										args
									}) => {
										const [message, memberId] = args;
										console.log('channel ', channelName, ', messsage: ', message.text, ', memberId: ', memberId);
										var msgObj = JSON.parse(message.text);
										console.log(msgObj);
										// 返回消息
										if (msgObj.destUid == this.uid) {
											// 发给谁
											this.destUid = msgObj.sourceUid;
											// 别人的房间号
											this.othersChannelId = msgObj.channel;
											this.$emit('waitForList', msgObj.destUid);
											// var pages = getCurrentPages();
											// var page = pages[pages.length - 1].route;
											// if (page == 'pages/consultationList') {
											// } else {
											switch (String(msgObj.action)) {
												case '1':
												
												
												 // = msgObj.sourceUid
												
												
												
												
												
												
													this.$refs.popup10.open();
													this.othersChannelId = msgObj.sourceUid;
													return
													break;
												case '2':
													console.log('挂断');
													this.$refs.popup10.close();
													break;
												case '3':
													console.log('接听');
													this.$refs.popup10.close();
													break;
											}
											// }
										}
									});
								})
								.catch(err => {
									console.error(err);
								});
						})
						.catch(err => {
							console.log(err);
						});
				} catch (err) {
					console.error(err);
				}
			},
			hujiao(e, n, r) {
				if (!this.rtm._logined) {
					console.log('Please Login First');
					return;
				}
				if (n) {
					this.destUid = n;
				}

				// 用户名房间号是写死的 没有
				var msg = JSON.stringify({
					sourceUid: this.uid,
					destUid: this.destUid,
					channelId: this.uid,
					action: e
				});

				var params = {
					appID: '9c3a9d03dac14f2cbcb2dcaa63d2a48a',
					channel: this.uid,
					uid: this.uid,
					channelMessage: msg
					// {"sourceUid":"11111","destUid":"1590796284","channelId":"991","doAction":"1"}
				};

				console.log(params);
				this.rtm
					.sendChannelMessage(params.channelMessage, this.publicChannelId)
					.then(() => {
						const view = ' text: account: ' + this.rtm.accountName + ' send : ' + params.channelMessage + ' channel: ' +
							params.channel;
						console.log(view);
						if (n) {
							uni.navigateTo({
								url: `./patientRecord?othersChannelId=${this.othersChannelId}&type=2`
							});
						}
						switch (e) {
							case 2:
								this.$refs.popup10.close();
								break;
							case 3:
								this.$refs.popup10.close();
								//  跳转视频页面
								uni.navigateTo({
									url: `./patientRecord?othersChannelId=${this.othersChannelId}`
								});
								break;
							default:

								break;
						}
					})
					.catch(err => {
						console.error(err);
					});
			}
		}
	};
</script>

<style lang="less" scoped>
	.uni-tip1 {
		background: url(../static/image/400091852@2x.png);
		width: 610.52px;
		height: 333.33px;
		font-size: 18.42px;
		color: #ffffff;
		padding: 61.4px;
		box-sizing: border-box;
		text-align: center;

		.uni-tip-title1 {}

		.uni-tip-group-button1 {
			display: flex;

			button {
				width: 132.45px;
				height: 37.71px;
				background: rgba(224, 58, 49, 1);
				border-radius: 48.24px;
				border: 1px solid rgba(250, 108, 108, 1);
				font-size: 14.03px;
				font-family: SourceHanSansCN-Medium, SourceHanSansCN;
				font-weight: 500;
				color: rgba(255, 255, 255, 1);
				margin-top: 42.1px;

				&:last-child {
					background: rgba(7, 206, 59, 1);
					border: 1px solid rgba(60, 246, 108, 1);
					margin-left: 54.38px;
				}
			}
		}
	}
</style>
